import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Main {
    //本质上升序列
    public static void main1(String[] args) {

    }

    //完全平方数
    public int numSquares(int n) {
        int key = (int)Math.sqrt(n) + 1;
        int[][] dp = new int[key+1][n+1];
        //int[][] dp = new int[count+1][n+1];
        for (int i = 0; i <= n; i++) {
            dp[0][i] = 0x3f3f3f3f;
        }
        for (int i = 1; i <= key; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = dp[i-1][j];
                if(j>=(i-1)*(i-1)) {
                    dp[i][j] = Math.min(dp[i][j], dp[i][j-(i-1)*(i-1)]+1);
                }
            }
        }
        return dp[key][n];
    }

    //有奖问答
    static int count = 0;
    public static void main2(String[] args) {
        dfs(0,0);
        System.out.println(count);
    }
    //7868928
    private static void dfs(int socer, int pos) {
        if(socer >= 100) {
            return;
        }
        if(socer == 70) {
            count++;
            //pos = 31;
            //return ;
        }
        if(pos >= 30) {
            return ;
        }

        dfs(socer+10, pos+1);
        dfs(0, pos+1);
        //System.out.println(7868928);
    }

    //R格式
    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String val = scanner.next();
        BigDecimal bigDecimal = new BigDecimal(val);
        for (int i = 0; i < n; i++) {
            bigDecimal = bigDecimal.multiply(new BigDecimal("2"));
        }
        int key = bigDecimal.toBigInteger().toString().length();
        System.out.println(bigDecimal.round(new MathContext(key)).toString());
    }

    //食堂
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int q = scanner.nextInt();
        while(q>0) {
            int count = 0;
            q--;
            int a2 = scanner.nextInt();
            int a3 = scanner.nextInt();
            int a4 = scanner.nextInt();
            int b4 = scanner.nextInt();
            int b6 = scanner.nextInt();
            while(a3>=2 && b6>=1) {
                count += 6;
                a3 -= 2;
                b6--;
            }
            while(a4>0 && b4>0) {
                a4--;
                b4--;
                count += 4;
            }
            while(a2>0 && a4>0 && b6>0) {
                b6--;
                a2--;
                a4--;
                count += 6;
            }
            while(a2>=2 && b4>0) {
                a2 -= 2;
                b4--;
                count += 4;
            }
            while(a2>=3 && b6>0) {
                a2 -= 3;
                b6--;
                count += 6;
            }
            while(a2>0 && a3>0 && b6>0) {
                b6--;
                a2--;
                a3--;
                count += 5;
            }
            while(a3>0 && b4>0) {
                a3--;
                b4--;
                count += 3;
            }
            while(a4>0 && b6>0) {
                a4--;
                b6--;
                count += 4;
            }
            while(a2>1 && b6>0) {
                a2 -= 2;
                b6--;
                count += 4;
            }
            while(a3>0 && b6>0) {
                a3--;
                b6--;
                count += 3;
            }
            while(a2>0 && b6>0) {
                a2--;
                b6--;
                count += 2;
            }
            while(a2>0 && b4>0) {
                a2--;
                b4--;
                count += 2;
            }
            System.out.println(count);
        }
    }

    //随机
    public static void main5(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int q = scanner.nextInt();
        while(q>0) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    int val = scanner.nextInt();
                }
            }
            Random random = new Random();
            int a = random.nextInt();
            //System.out.println(a);
            if(a%2==0) {
                System.out.println("No");
            }else {
                System.out.println("Yes");
            }
            q--;
        }
    }

    //类斐波那契循环数
    public static void main(String[] args) {

        for (int i = 10000000; i >= 197; i--) {
            if(check(i)==true) {
                System.out.println(i);
                //System.out.println(6065280);
                break;
            }
        }
    }

    private static boolean check(int i) {
        String str = String.valueOf(i);
        int val = i;
        int n = str.length();
        int sum = 0;
        List<Integer> list = new LinkedList<>();
        while(val>0) {
            sum += val%10;
            list.add(val%10);
            val /= 10;
        }
        list.add(sum);
        while(true) {
            if(list.get(list.size()-1) >= i) {
                if(list.get(list.size()-1) == i) {
                    System.out.println(list.get(list.size()-1));
                    return true;
                }else {
                    return false;
                }
            }
            if(i == 6065280) {
                System.out.println(list.get(list.size()-1));
                System.out.println(list.toString());
            }

            list.add(list.get(list.size()-1)*2 - list.get(0));
            list.remove(0);
        }
    }
}
